package data;

import entity.Reader;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @title: ReaderDao
 * @projectName BMS
 * @description: ReaderDao
 * @date 2024/12/21 11:13
 */
public class ReaderDao {
    /**
     * 通过读者编号 获取读者对象
     * @param id
     * @return
     */
    public static Reader getReaderById(String id) {
        Reader reader = null;
        if(id != null){
            String sql = "select * from reader where id = " + id;
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                if(rs.next()){
                    reader = new Reader();
                    reader.setId(rs.getString("id"));
                    reader.setName(rs.getString("name"));
                    reader.setSex(rs.getString("sex"));
                    reader.setType(rs.getString("type"));
                    reader.setMax_num(rs.getInt("max_num"));
                    reader.setDays_num(rs.getInt("days_num"));
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return reader;
    }

    /**
     * 通过sql查询语句 获取读者对象列表
     * @param sql
     * @return
     */
    public static List<Reader> selectReaderList(String sql) {
        List<Reader> list = new ArrayList<Reader>();
        if(sql != null){
            Reader reader = null;
            ResultSet rs = BaseDao.executeQuery(sql);
            try{
                while(rs.next()){
                    reader = new Reader();
                    reader.setId(rs.getString("id"));
                    reader.setName(rs.getString("name"));
                    reader.setSex(rs.getString("sex"));
                    reader.setType(rs.getString("type"));
                    reader.setMax_num(rs.getInt("max_num"));
                    reader.setDays_num(rs.getInt("days_num"));
                    list.add(reader);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 获取所有读者对象列表
     * @return
     */
    public static List<Reader> selectReaderList() {
        String sql = "select * from reader";
        return selectReaderList(sql);
    }

    /**
     * 按照字符串类型属性值查询条件 获取读者对象列表
     * @param field
     * @param value
     * @return
     */
    public static List<Reader> selectReaderList(String  field, String value) {
        String sql = "select * from reader";
        if((value != null & value.length() > 0) && field != null){
            sql = "select * from reader where " + field + " like '%" + value + "%'";
        }
        return selectReaderList(sql);
    }

    /**
     * 根据整型数据查询条件  获取读者对象列表
     * @param field
     * @param value
     * @return
     */
    public static List<Reader> selectReaderList(String field, int value) {
        String sql = "select * from reader";
        if(field != null && value != 0){
            sql = "select * from reader where " + field + " = " + value;
        }
        return selectReaderList(sql);
    }

    /**
     * 添加新读者
     * @param reader
     * @return
     */
    public static int insertReader(Reader reader) {
        if(reader != null){
            String id = reader.getId();
            String name = reader.getName();
            String sex = reader.getSex();
            String type = reader.getType();
            int max_num = reader.getMax_num();
            int days_num = reader.getDays_num();
            String sql = "inser into reader values('" + id + "','" + name + "','" + sex + "','" + type + "'," + max_num + "," + days_num + ")";
            return BaseDao.executeUpdate(sql);
        }else{
            return -1;
        }
    }

    /**
     * 修改读者信息
     * @param reader
     * @return
     */
    public static int updateReader(Reader reader) {
        if(reader != null){
            String id = reader.getId();
            String name = reader.getName();
            String sex = reader.getSex();
            String type = reader.getType();
            int max_num = reader.getMax_num();
            int days_num = reader.getDays_num();
            String sql = "update reader set name='" + name + "',sex='" + sex + "',type='" + type + "',max_num=" + max_num + ",days_num=" + days_num + " where id='" + id + "'";
            return BaseDao.executeUpdate(sql);
        }else{
            return -1;
        }
    }

    /**
     * 删除指定读者信息
     * @param id
     * @return
     */
    public static int deleteReader(String id) {
        String sql = "delete from reader where id = '" + id + "'";
        return BaseDao.executeUpdate(sql);
    }

    /**
     * 清空所有读者信息
     */
    public static void emptyReader() {
        String sql = "delete from reader";
        BaseDao.executeUpdate(sql);
    }
}
